package com.activity;

import android.content.DialogInterface;
import android.os.Bundle;


import com.activity.bean.UserBean;
import com.api.LoginApi;
import com.api.param.AddFaceParam;
import com.api.param.UpdateUserParam;
import com.api.response.BaseResponsePredicate;
import com.baidu.idl.face.platform.FaceStatusEnum;
import com.baidu.idl.face.platform.ui.FaceDetectActivity;
import com.example.apple.ddhr_new.utils.ActivityUtil;
import com.example.http.BuildFactory;
import com.example.http.HttpClient;
import com.google.gson.Gson;
import com.kaopiz.kprogresshud.KProgressHUD;

import org.greenrobot.eventbus.EventBus;

import java.util.HashMap;
import java.util.Map;

import event.MessageEvent;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

public class FaceDetectExpActivity extends FaceDetectActivity {

    private DefaultDialog mDefaultDialog;
    private KProgressHUD HUD;
    public void showLoading(boolean show) {

        if (HUD == null) {
            HUD = KProgressHUD.create(this)
                    .setStyle(KProgressHUD.Style.SPIN_INDETERMINATE)
                    .setCancellable(false)
                    .setDimAmount(0.1f);
        }
        if (show) {
            HUD.show();
        } else {
            HUD.dismiss();
        }
    }
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public void onDetectCompletion(FaceStatusEnum status, String message, HashMap<String, String> base64ImageMap) {
        super.onDetectCompletion(status, message, base64ImageMap);
        if (status == FaceStatusEnum.OK && mIsCompletion) {
//            showMessageDialog("人脸图像采集", "采集成功");

            AddFaceParam addFaceParam =new AddFaceParam();
            addFaceParam.user_id=App.getInstance().getAccount().getUserid();
            addFaceParam.image_type="BASE64";
            addFaceParam.image = base64ImageMap.get("bestImage0");
            addFaceParam.liveness_control = "HIGH";
            addFaceParam.access_token = App.getInstance().authBean.getAccess_token();
            addFaceParam.quality_control = "HIGH";

            Gson gson = new Gson();
            String mapJson =  gson.toJson(addFaceParam);
            showLoading(true);

            Map<String,String> map = gson.fromJson(mapJson,Map.class);
            HttpClient.getInstance().getBuilder(HttpClient.BAIDU_FACE_URL).build().create(LoginApi.class).searchFace(map, addFaceParam.access_token)
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(res -> {

                        showLoading(false);
                        if(res.getError_code()==0&&res.getResult().getUser_list()!=null&&res.getResult().getUser_list().size()>0){

                            if(res.getResult().getUser_list().get(0).getScore()>80){
                                finish();
                                EventBus.getDefault().post(new MessageEvent(MessageEvent.MESSAGE_EVENT_FACE_SUCCESS));
                            }else {
                                showMessageDialog("认证失败", "识别失败");
                            }

                        }else {
                            showMessageDialog("认证失败", "识别失败");
                        }





                    },throwable -> {

                        showLoading(false);
                        showMessageDialog("", throwable.getMessage());

                    });

        } else if (status == FaceStatusEnum.Error_DetectTimeout ||
                status == FaceStatusEnum.Error_LivenessTimeout ||
                status == FaceStatusEnum.Error_Timeout) {
            showMessageDialog("人脸图像采集", "采集超时");
        }
    }

    private void showMessageDialog(String title, String message) {
        if (mDefaultDialog == null) {
            DefaultDialog.Builder builder = new DefaultDialog.Builder(this);
            builder.setTitle(title).
                    setMessage(message).
                    setNegativeButton("确认",
                            new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                    mDefaultDialog.dismiss();
                                    finish();

                                }
                            });
            mDefaultDialog = builder.create();
            mDefaultDialog.setCancelable(true);
        }
        mDefaultDialog.dismiss();
        mDefaultDialog.show();
    }

    @Override
    public void finish() {
        super.finish();
    }

}
